Repository navigation
defer GC work while in cabi_realloc - #13
Conversation
Allocations made from `cabi_realloc` may start or finish a GC cycle, and the Go GC calls imports while doing so, which the component model forbids. The monotonic clock is already paused via the adapter, but there are two remaining traps with `cannot leave component instance`: - gcMarkTermination reads the realtime clock (walltime -> wall-clock.now) - gcStart's startTheWorldWithSema polls timers (netpoll -> poll_oneoff) This shows up when a host lowers large results into the guest, e.g. a list of ~40k records (~80k consecutive `cabi_realloc` calls). Wrap the allocation in `runtime.procPin`/`procUnpin`. With the M's lock count raised, the runtime neither starts a GC cycle nor performs assist work, so any GC work is deferred to the next allocation made outside `cabi_realloc`. This also keeps the GC from switching to other goroutines here. The runtime explicitly supports linking to these functions (golang/go#67401). See bytecodealliance/componentize-go#77 and bytecodealliance/wasmtime#14318. Signed-off-by: Bailey Hayes <bailey@cosmonic.com>
|
Thanks for this, @ricochet! Seems like a promising approach.
I wonder if we could trigger a GC next time the guest is entered following a |
|
@ricochet are you planning to come back to this? I do think this is a promising approach, and I'm happy to take over this PR if you don't have time for it. |
If you're able to take it over the line, please feel free! |
This pulls in bytecodealliance/go-pkg#13, fixing bytecodealliance/wasmtime#14318 (which, despite being entered as a Wasmtime issue, ended up being fixed in `go-pkg`).
go.bytecodealliance.org/pkg v0.4.0 lets the Go garbage collector start a cycle inside cabi_realloc. A GC cycle calls imports (clocks, I/O polling), which the component model forbids there, so the component traps. It shows up once a single value is big enough to trigger GC: a list<record> of ~40k records traps while 20k works, and reading one 8.8 MiB NATS KV value traps with gcStart and cabi_realloc in the backtrace. v0.4.1 (bytecodealliance/go-pkg#13) pins the M while allocating, deferring GC work until the allocation returns. - component/ and plugin/ require v0.4.1. - Every example and the template pin v0.4.1 too. They build against the released SDK v0.1.6, which still names v0.4.0, so MVS needs the pin to pick the fix; and the air-gapped image seeds GOMODCACHE from these modules alone, so without it the in-repo SDK build would go offline short of v0.4.1. - airgap/versions.env and BUILDING.md record the new pin and why it is the floor; check-versions.sh now fails if any module names another go-pkg version. No binding regeneration: generated code names go-pkg by import path only. componentize-go stays at v0.4.3. Signed-off-by: Bailey Hayes <bailey@cosmonic.com>
This change defers GC work while in cabi_realloc. I ran into the same issue as bytecodealliance/componentize-go#77. This change fixes the issue without patching the adapter. Although I think now that we've already made the workarounds, it's still worth landing bytecodealliance/wasmtime#14319. These can be complementary.
Allocations made from
cabi_reallocmay start or finish a GC cycle, and the Go GC calls might call imports which trap. The monotonic clock is already paused via the adapter, but there are two remaining traps withcannot leave component instance:The timer poll happens when a cycle starts:
This shows up when a host lowers large results into the guest, e.g. a list of ~40k records (~80k consecutive
cabi_realloccalls).cabiRealloc now calls runtime.procPin() before allocating and runtime.procUnpin() after. With the M's lock count raised, the runtime neither starts a GC cycle nor performs assist work, so any GC work is deferred to the next allocation made outside
cabi_realloc. This also keeps the GC from switching to other goroutines here. The allocations are still charged as assist debt, so the pacer catches up then.This covers both traps above as well as any other import a GC cycle could reach from inside cabi_realloc, instead of patching the adapter one import at a time. For example, an assist that parks could let another goroutine run and call an import.
The runtime explicitly allows linking to procPin/procUnpin and commits to keeping their signatures (golang/go#67401). They are push linknames, so this doesn't depend on -checklinkname=0.
Follow-up to bytecodealliance/componentize-go#56. Related: bytecodealliance/componentize-go#77, bytecodealliance/wasmtime#14318.